home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSEXCP.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  10KB  |  332 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dosexcp.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include <errno.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <string.h>
  39. #include <dos.h>
  40. #include "dossys.h"
  41. #include "dosinsn.h"
  42. #include "dosexcp.h"
  43.  
  44. /* It would be nice to be able to use something akin to Zortech's int_intercept
  45.    to get control of trap handlers, but Zortech does not provide that ability.
  46.    In fact, it shadows the exception numbers with DOS interrupts (for compatibility),
  47.    but does not map the traps to an accessible region.
  48.    In the meantime, exceptions are only caught under DPMI.
  49. */
  50.  
  51. #if 0
  52.  
  53. #include <int.h>
  54.  
  55. static unsigned long *
  56. DEFUN (store_trap_data, (trap_stack_ptr, intno, code, pd), 
  57.        unsigned long ** trap_stack_ptr 
  58.        AND unsigned char intno AND unsigned char code
  59.        AND struct INT_DATA * pd)
  60. {
  61.   unsigned long 
  62.     * trap_stack,
  63.     * trapped_stack;
  64.   
  65.   union
  66.   { 
  67.     unsigned long long_value;
  68.     struct
  69.     { 
  70.       unsigned char code;
  71.       unsigned char ss_is_ds;
  72.       unsigned char intno;
  73.       unsigned char padding;
  74.     } byte_values;
  75.   } code_word;
  76.  
  77.   trap_stack = (* trap_stack_ptr);
  78.   trapped_stack = ((unsigned long) pd->regs.oldstack_off);
  79.  
  80.   code_word.byte_values.code = code;
  81.   code_word.byte_values.intno = intno;
  82.   code_word.byte_values.ss_is_ds = (pd->oldstack_seg == pd->sregs.ds);
  83.   code_word.byte_values.padding = 0;
  84.  
  85.   *--trap_stack = code_word.long_value;
  86.   *--trap_stack = pd->regs.e.eax;
  87.   *--trap_stack = pd->regs.e.ecx;
  88.   *--trap_stack = pd->regs.e.edx;
  89.   *--trap_stack = pd->regs.e.ebx;
  90.  
  91.   /* The following checks whether there was a ring change when the
  92.      interrupt was taken.  If there was, the old SP is pushed on the
  93.      exception trap frame which lives on the stack of the new
  94.      privilege level, otherwise the trap frame was pushed on the 
  95.      interrupted stack, which is shared by the low-level handler.
  96.      Compare the PL of the two PCs.
  97.    */
  98.   *--trap_stack = (((pd->regs.sregs.cs & 0x3) == (trapped_stack[3] & 0x3))
  99.                    ? (trapped_stack + 5)
  100.            : (trapped_stack[5]));    /* esp */
  101.   *--trap_stack = trapped_stack[0];        /* ebp */
  102.   *--trap_stack = pd->regs.e.esi;
  103.   *--trap_stack = pd->regs.e.edi;
  104.   *--trap_stack = trapped_stack[2];        /* eip */
  105.   *--trap_stack = pd->regs.e.flags;
  106.   (* trap_stack_ptr) = trap_stack;
  107.   return (trapped_stack + 2);
  108. }
  109.  
  110. static int
  111. DEFUN (dosx_trap_handler, (intno, pd), 
  112.        unsigned char intno AND struct INT_DATA * pd)
  113. {
  114.   extern void asm_trap_handler ();
  115.   extern unsigned long
  116.     * asm_trap_stack,
  117.     * asm_trap_stack_limit,
  118.     * asm_trap_stack_base;
  119.   unsigned long * pc_loc;
  120.   int code;
  121.  
  122.   code = 0;
  123.   if (asm_trap_stack <= asm_trap_stack_limit)
  124.   {
  125.     /* Lose badly.  Too many nested traps. */
  126.     asm_trap_stack = asm_trap_stack_base;
  127.     code = 1;
  128.   }
  129.   pc_loc = store_trap_data (&asm_trap_stack, intno, code, pd);
  130.   (* pc_loc) = ((unsigned long) asm_trap_handler);
  131.   return (0);
  132. }
  133.  
  134. #define DEFINE_TRAP_HANDLER(name,intno)        \
  135. extern int EXFUN (name, (struct INT_DATA *));    \
  136. int                        \
  137. DEFUN (name, (pd), struct INT_DATA * pd)    \
  138. {                        \
  139.   return (dosx_trap_handler (intno, pd));    \
  140. }
  141.  
  142. DEFINE_TRAP_HANDLER (handle_integer_divide_by_0, DOS_INTVECT_DIVIDE_BY_0)
  143. DEFINE_TRAP_HANDLER (handle_overflow, DOS_INTVECT_OVERFLOW)
  144. DEFINE_TRAP_HANDLER (handle_bounds_check, DOS_INTVECT_PRINT_SCREEN)
  145. DEFINE_TRAP_HANDLER (handle_invalid_opcode, DOS_INVALID_OPCODE)
  146. /* And many more friends. */
  147.  
  148. #endif /* 0 */
  149.  
  150. int
  151. DPMI_get_exception_vector (unsigned exception,
  152.                unsigned short * cs_selector,
  153.                unsigned * code_offset)
  154. {
  155.   union REGS regs1, regs2;
  156.  
  157.   if (exception > 0x1f)
  158.   {
  159.     errno = EINVAL;
  160.     return (DOS_FAILURE);
  161.   }
  162.   regs1.e.eax = 0x202;
  163.   regs1.e.ebx = exception;
  164.   int86 (0x31, ®s1, ®s2);
  165.   if ((regs2.e.flags & 1) != 0)
  166.   {
  167.     errno = EINVAL;
  168.     return (DOS_FAILURE);
  169.   }
  170.   * cs_selector = regs2.x.cx;
  171.   * code_offset = regs2.e.edx;
  172.   return (DOS_SUCCESS);
  173. }
  174.  
  175. int
  176. DPMI_set_exception_vector (unsigned exception,
  177.                unsigned short cs_selector,
  178.                unsigned code_offset)
  179. {
  180.   union REGS regs;
  181.   struct SREGS sregs;
  182.   
  183.   if (exception > 0x1f)
  184.   {
  185.     errno = EINVAL;
  186.     return (DOS_FAILURE);
  187.   }
  188.   segread (& sregs);
  189.   regs.e.eax = 0x203;
  190.   regs.e.ebx = exception;
  191.   regs.e.ecx = cs_selector;
  192.   regs.e.edx = code_offset;
  193. #ifdef _DOSEXCP_DEBUG
  194.   if (exception == DOS_EXCP_General_protection)
  195.   {
  196.     printf ("About to do int86x for excp %d.\n", DOS_EXCP_General_protection);
  197.     printf ("sregs.ds = 0x%04x; sregs.es = 0x%04x; sregs.fs = 0x%04x\n",
  198.             sregs.ds, sregs.es, sregs.fs);
  199.     printf ("sregs.gs = 0x%04x; sregs.ss = 0x%04x; sregs.cs = 0x%04x\n",
  200.             sregs.gs, sregs.ss, sregs.cs);
  201.     printf ("regs.e.eax = 0x%08x; regs.e.ebx = 0x%08x; regs.e.ecx = 0x%08x\n",
  202.             regs.e.eax, regs.e.ebx, regs.e.ecx);
  203.     printf ("regs.e.edx = 0x%08x\n", regs.e.edx);
  204.     fflush (stdout);
  205.     sleep (1);
  206.   }
  207. #endif
  208.   int86x (0x31, ®s, ®s, &sregs);
  209.   if ((regs.e.flags & 1) != 0)
  210.   {
  211.     errno = EINVAL;
  212.     return (DOS_FAILURE);
  213.   }
  214.   return (DOS_SUCCESS);
  215. }
  216.  
  217. static void *
  218. make_DPMI_exception_trampoline (unsigned exception,
  219.                 void ((*funcptr)
  220.                       (unsigned,
  221.                        unsigned,
  222.                        struct sigcontext *)),
  223.                 void * stack)
  224. {
  225.   void DPMI_exception_method (void);
  226.   void DPMI_GP_exception_method (void);
  227.   void * trampoline;
  228.   int size;
  229.   INSN_DECLS();
  230.  
  231.   size = ((exception == DOS_EXCP_General_protection) ? 8 : 6);
  232.   trampoline = (malloc (TRAMP_SIZE (size)));
  233.   if (trampoline == ((void *) NULL))
  234.   {
  235.     errno = ENOMEM;
  236.     return ((void *) NULL);
  237.   }
  238.   
  239.   INIT_INSNS (trampoline);
  240.  
  241.   PUSH_INSN (exception);
  242.   PUSH_INSN (getDS ());
  243. #if 0
  244.   PUSH_INSN (getCS ());
  245. #else
  246.   PUSH_INSN (0);        /* Use same CS and near calls and returns */
  247. #endif
  248.   PUSH_INSN (funcptr);
  249.   PUSH_INSN (getDS ());        /* Assumed to be on Heap if not null! */
  250.   PUSH_INSN (stack);
  251.   if (exception == DOS_EXCP_General_protection)
  252.   {
  253.     unsigned short previous_cs;
  254.     unsigned previous_eip;
  255.     
  256.     if ((DPMI_get_exception_vector (exception, & previous_cs, & previous_eip))
  257.     != DOS_SUCCESS)
  258.     {
  259.       free (trampoline);
  260.       errno = EACCES;
  261.       return ((void *) NULL);
  262.     }
  263. #ifdef _DOSEXCP_DEBUG
  264.     printf ("Previous CS = 0x%04x; Previous EIP = 0x%08x\n",
  265.         previous_cs, previous_eip);
  266.     printf ("Current CS = 0x%04x; Current SS = 0x%04x; Current DS = 0x%04x\n",
  267.         (getCS ()), (getSS ()), (getDS ()));
  268.     fflush (stdout);
  269. #endif
  270.     PUSH_INSN (previous_cs);
  271.     PUSH_INSN (previous_eip);
  272.     JMP_INSN (DPMI_GP_exception_method);
  273.   }
  274.   else
  275.     JMP_INSN (DPMI_exception_method);
  276.  
  277.   HLT_INSNS (size);
  278.  
  279.   return (trampoline);
  280. }
  281.  
  282. int
  283. DPMI_set_exception_handler (unsigned exception,
  284.                 void ((*funcptr)
  285.                   (unsigned,
  286.                    unsigned,
  287.                    struct sigcontext *)),
  288.                 void * stack)
  289. {
  290.   void * handler;
  291.   
  292.   if (exception > 0x1f)
  293.   {
  294.     errno = EINVAL;
  295.     return (DOS_FAILURE);
  296.   }
  297.   handler = (make_DPMI_exception_trampoline (exception, funcptr, stack));
  298.   if ((handler == ((void *) NULL))
  299.       || ((DPMI_set_exception_vector (exception,
  300.                       (getCS ()),
  301.                       ((unsigned) handler)))
  302.       != DOS_SUCCESS))
  303.   {
  304.     int saved_errno = errno;
  305.  
  306.     if (handler != ((void *) NULL))
  307.       free (handler);
  308.     errno = saved_errno;
  309.     return (DOS_FAILURE);
  310.   }
  311.   return (DOS_SUCCESS);
  312. }
  313.  
  314. /* This assumes that it is undoing the effects of DPMI_set_exception_handler */
  315.  
  316. int
  317. DPMI_restore_exception_handler (unsigned exception,
  318.                 unsigned short cs_selector,
  319.                 unsigned code_offset)
  320. {
  321.   unsigned short current_cs;
  322.   unsigned current_eip;
  323.   
  324.   if (((DPMI_get_exception_vector (exception, & current_cs, & current_eip))
  325.        != DOS_SUCCESS)
  326.       || ((DPMI_set_exception_vector (exception, cs_selector, code_offset))
  327.       != DOS_SUCCESS))
  328.     return (DOS_FAILURE);
  329.   free ((void *) current_eip);
  330.   return (DOS_SUCCESS);
  331. }
  332.